/**
 * 首页管理初始化
 */
var Apphome = {
    id: "ApphomeTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Apphome.initColumn = function () {
    return [
        {field: 'selectItem', checkbox: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        // {title: '年龄段', field: 'CategoryClassName',  align: 'center', valign: 'middle'},
        {title: '显示标题', field: 'viewTitle',  align: 'center', valign: 'middle'},
        {title: '页面分类', field: 'PageName',  align: 'center', valign: 'middle'},
        //{title: '位置', field: 'name',  align: 'center', valign: 'middle'},
        //{title: '分类', field: 'type',  align: 'center', valign: 'middle'},
        // {title: '专辑', field: 'zhuanji',  align: 'center', valign: 'middle'},
        {title: '专辑', field: 'zhuanjiName',  align: 'center', valign: 'middle'},
        {title: '分类', field: 'fenleiName',  align: 'center', valign: 'middle'},
        {title: '图片', field: 'img',  align: 'center', valign: 'middle',
            formatter : function (value, row, index) {
                return "<img style='width: 50px;height: 50px;' src='"+value+"' alt=''>"
            }},
        {title: '简单注释', field: 'desc',  align: 'center', valign: 'middle'},
        {title: '排序', field: 'sort',  align: 'center', valign: 'middle'},
    ];
};

/**
 * 检查是否选中
 */
Apphome.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Apphome.seItem = selected[0];
        return true;
    }
};
/**
 * 批量检查是否选中
 */
Apphome.batchCheck = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Apphome.seItem = selected;
        return true;
    }
};
/**
 * 点击添加首页
 */
Apphome.openAddApphome = function () {
    var index = layer.open({
        type: 2,
        title: '添加首页',
        area: ['800px', '650px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/apphome/apphome_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看首页详情
 */
Apphome.openApphomeDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '首页详情',
            area: ['800px', '650px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/apphome/apphome_update/' + Apphome.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除首页
 */
Apphome.delete = function () {
    if (this.batchCheck()) {
        var ajax = new $ax(Feng.ctxPath + "/apphome/delete", function (data) {
            Feng.success("删除成功!");
            Apphome.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        var ids = "";
        for (var i = 0;i < Apphome.seItem.length;i++) {
            if(i < Apphome.seItem.length - 1) {
                ids += Apphome.seItem[i].id + ",";
            } else {
                ids += Apphome.seItem[i].id;
            }
        }
        ajax.set("ids",ids);
        ajax.start();
    }
};

/**
 * 查询首页列表
 */
Apphome.search = function () {
    var queryData = {};
    queryData['viewTitle'] = $("#viewTitle").val();
    queryData['classtypeId'] = $("#classtypeId").val();
    queryData['pageclassId'] = $("#pageclassId").val();
    Apphome.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = Apphome.initColumn();
    var table = new BSTable(Apphome.id, "/apphome/list", defaultColunms);
    table.setPaginationType("client");
    Apphome.table = table.init();
});
